import mongoose from 'mongoose'
const Movie=mongoose.model('Movie')

const getAllMovies=async(type)=>{
    let query={}
    if(type){
        query.classification={
            $in:new RegExp(type , "gi")
        }
    }
  
    const movies=await Movie.find(query)

    return movies
}
 const getMovieDetail = async (id) => {
    const movie = await Movie.findOne({_id: id})
  
    return movie
  }
  const getAllType=async()=>{
      const types=await Movie.find({},'classification')
        var data=''
        const s=new Set()
      for(let i=0;i<types.length;i++){
        data+=types[i].classification+'/'
      }
      let dataList=data.substring(0,data.length-1).split('/')
      dataList.forEach(element => {
          s.add(element)
      });
     
      return [...s]
      
  }
   const findAndRemove = async (id) => {
    const movie = await Movie.findOne({_id: id})
  
    if (movie) {
      await movie.remove()
    }
  }
export{
    findAndRemove,
    getAllMovies,
    getMovieDetail,
    getAllType
  }